package com.kaikeba.wx.controller;

import com.kaikeba.bean.BootStrapTableExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateformatUtil;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class ExpressController {
    @ResponseBody("/wx/findExpressByUserPhone.do")
    public String findByUserPhone(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getUserPhone();
        List<Express> list = ExpressService.findByUserPhone(userPhone);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String intime = DateformatUtil.format(e.getInTime());
            String outtime = e.getOutTime() == null ? "未出库" : DateformatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            String code = e.getCode() == null ? "已失效" : e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUsername(), e.getUserPhone(), e.getCompany(), code, intime, outtime, status, e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if (list.size()==0){
            msg.setStatus(-1);
            msg.setResult("暂无快递信息");
        }else {
            msg.setStatus(0);
            Stream<BootStrapTableExpress> status0Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("待取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2)->{
                long o1Time = DateformatUtil.toTime(o1.getInTime());
                long o2Time = DateformatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });
            Stream<BootStrapTableExpress> status1Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("已取件")){
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2)->{
                long o1Time = DateformatUtil.toTime(o1.getInTime());
                long o2Time = DateformatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });;

            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            Map data = new HashMap<>();
            data.put("status0",s0);
            data.put("status1",s1);
            msg.setData(data);
            msg.setResult("数据查询成功");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/userExpressList.do")
    public String expressList(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        //未取件快递集合
        List<Express> list = ExpressService.findByUserPhoneAndStatus(userPhone,0);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String intime = DateformatUtil.format(e.getInTime());
            String outtime = e.getOutTime() == null ? "未出库" : DateformatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            String code = e.getCode() == null ? "已失效" : e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUsername(), userPhone, e.getCompany(), code, intime, outtime, status, e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if (list.size()==0){
            msg.setStatus(-1);
            msg.setResult("未查询到快递");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list2);
        }
        //扫码了
        System.out.println(list2);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/addExpress.do")
    public String addExpress(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        String company = request.getParameter("company");
        User wxUser = UserUtil.getWxUser(request.getSession());
        String sysPhone = wxUser.getUserPhone();
        Express express = new Express(number,userName,userPhone,company,sysPhone);
        boolean flag = ExpressService.insert(express);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("快递信息录入成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("录入失败，请稍后重试");
        }
        return JSONUtil.toJSON(msg);
    }
}
